<?php

/**
 * @copyright: ©2024 永州微聚商 网络科技有限公司
 * @link: https://www.weiJuyunke.com
 */

namespace app\mobile\controller;


use app\admin\model\ActivityInvitationLog;
use app\admin\model\ConsumerClaimLog;
use app\admin\model\CouponConsumer;
use app\admin\model\CouponOrder;
use app\admin\model\DonateLog;
use app\BaseController;
use think\App;


/**
 * 用户操作
 *
 * @author Administrator
 *
 */
class Goods extends BaseController
{

    public function saveStoreGoods(){
        $request = $this->request;
        $param = $request->param();

        $goodsModel  =  new \app\admin\model\Goods();

        $goodsData = [
            'name' => isset($param['name']) ?$param['name']:'' ,
            'platform_id'=> $this->request->platform_id,
            'store_id' => $param['store_id'],
            'goods_main_img' =>    isset($param['goods_main_img']) ?$param['goods_main_img']:'' ,
            'goods_introduction_img' =>    isset($param['goods_introduction_img']) ?$param['goods_introduction_img']:'' ,
            'original_price' =>  isset($param['original_price']) ?$param['original_price']:0 ,
            'price' =>    isset($param['price']) ?$param['price']:0 ,
            'inventory' =>    isset($param['inventory']) ?$param['inventory']:'' ,
            'sales' =>  $param['sales'],
        ];
        if (isset($param['id']) && $param['id']){
            $goodsModel->where(['id' => $param['id']])->update($goodsData);
            return result(0, '修改成功');

        }else{
            $goodsModel->save($goodsData);
            return result(0, '添加成功');

        }


    }

    public function goodsList(){
        $request = $this->request;
        $param = $request->param();

        $goodsModel  =  new \app\admin\model\Goods();
        $where[]= ['goods.is_delete','=',0];
        $where[]= ['platformInfo.expiration_time','>',time()];
        if (!isset($param['store_id']) || empty($param['store_id']) ){
            return result(-1, '请选择门店');
        }
        if (isset($param['store_id']) &&  $param['store_id']){
            $where[] =['store_id','=',$param['store_id']];
        }
        if (isset($param['status']) &&  $param['status']){
            $where[] =['goods.status','=',$param['status']];
        }
        if (isset($param['name']) &&  $param['name']){
            $where[] =['goods.name','like','%'.$param['name'].'%'];
        }
        $pindex = max(1, intval(input('page')));
        $psize = empty(input('psize')) ? 10 : intval(input('psize'));
        $total = $goodsModel
            ->withJoin(['storeInfo','platformInfo'], 'LEFT')
            ->where($where)->count();
        $list = $goodsModel
            ->withJoin(['storeInfo','platformInfo'], 'LEFT')
            ->where($where)->order('create_time', 'desc')
            ->limit(($pindex - 1) * $psize, $psize)
            ->select();
        foreach($list as $key => $value){
            $list[$key]['total_sales'] = $value['sales'] + $value['real_sales'];

        }
        $totalPage = ceil($total / $psize);
        return result(0, 'success', array(
            'list' => $list,
            'total' => $total,
            'pindex' => $pindex,
            'psize' => $psize,
            'totalPage' => $totalPage
        ));
    }

    public function updateStatus(){
        $request = $this->request;
        $param = $request->param();

        $goodsModel  =  new \app\admin\model\Goods();
        $goodsModel->where(['id' => $param['id']])->update(['status' => $param['status']]);
        if ($param['status'] == 1){
            return result(0, '上架成功');

        }else{
            return result(0, '下架成功');

        }
    }


    public function deleteGoods(){
        $request = $this->request;
        $param = $request->param();
        $goodsModel  =  new \app\admin\model\Goods();
        $goodsModel->where(['id' => $param['id']])->update(['is_delete' => 1]);
        return result(0, '删除成功');


    }


    public function goodsInfo(){
        $request = $this->request;
        $param = $request->param();

        $goodsModel  =  new \app\admin\model\Goods();
        $info = $goodsModel->where(['id' =>$param['id'] ])->find();
        $info['total_sales'] = $info['sales'] + $info['real_sales'];
        return result(0, '商品详情',$info);


    }


}